// 路由守卫
import router from ".";
import { useAppUserStoreWithOut } from "../stores/appUser";

router.beforeEach(async (to, from) => {
  const store = useAppUserStoreWithOut();
  if (
    // 检查用户是否已登录
    (!store.token || store.token == "") &&
    // ❗️ 避免无限重定向
    to.name !== "login"
    && to.name !== "components" //第三方组件
  ) {
    // 将用户重定向到登录页面
    return { name: "login" };
  }

  if (to.meta?.isRequreManager && !store.currentUser.isManager) {
    return { name: "notfound" };
  }

});

router.afterEach((to, from, failure) => {
  if (!failure && failure) {
    router.push({
      path: "error",
    });
  }
});
